﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ENail.Domain.Core;
using ENail.Domain.Core.Contract.Services;
using Equation.Enterprise.Framework.Domain.Data;
using Equation.Enterprise.Utilities.DesignByContract;

namespace ENail.ApplicationServices
{
    public class RegistrationService : IRegistrationService
    {
        private readonly IRepository<Registration> _registrationRepository;

        public RegistrationService(IRepository<Registration> registrationRepository)
        {
            Check.Require(registrationRepository != null);

            this._registrationRepository = registrationRepository;
        }

        /// <summary>
        /// Helps view all registrations on list view
        /// </summary>
        /// <returns>List of all registrations</returns>
        public IList<Registration> GetAllRegistrations()
        {
            var registrations = _registrationRepository.List();

            Check.Ensure(registrations != null);

            return registrations.ToList();
        }

        /// <summary>
        /// Helps view registrations for search view
        /// </summary>
        /// <param name="where">Lambda condition to get registrations</param>
        /// <returns></returns>
        public IList<Registration> GetRegistrations(Expression<Func<Registration, bool>> @where)
        {
            var registrations = _registrationRepository.List(@where);

            Check.Ensure(registrations != null);

            return registrations.ToList();
        }

        /// <summary>
        /// Helps view active registrations only
        /// </summary>
        /// <returns>All active registrations</returns>
        public IList<Registration> GetAllActiveRegistrations()
        {
            var registrations = _registrationRepository.List(x => x.IsActive);
            Check.Ensure(registrations != null);

            return registrations.ToList();
        }

        /// <summary>
        /// Helps view details of an registration
        /// </summary>
        /// <param name="id">Identity of registration</param>
        /// <returns>Registration with specified identity</returns>
        public Registration GetRegistration(int id)
        {
            Check.Require(id > 0);

            Registration registration = _registrationRepository.Get(id);
            Check.Ensure(registration != null);

            return registration;
        }

        /// <summary>
        /// Insert an registration from view
        /// </summary>
        /// <param name="registration">Registration to be inserted</param>
        /// <returns>Number of records affected</returns>
        public int InsertRegistration(Registration registration)
        {
            Check.Require(registration != null);

            _registrationRepository.Add(registration);
            return _registrationRepository.SaveChanges();
        }

        /// <summary>
        /// Update an registration from view
        /// </summary>
        /// <param name="registration">Registration to be updated</param>
        /// <returns>Number of records affected</returns>
        public int UpdateRegistration(Registration registration)
        {
            Check.Require(registration != null);

            _registrationRepository.Update(registration);
            return _registrationRepository.SaveChanges();
        }

        /// <summary>
        /// Helps to inactive an registration
        /// </summary>
        /// <param name="registration">Registration to be inactive</param>
        /// <returns>Number of records affected</returns>
        public int DeActivateRegistration(Registration registration)
        {
            Check.Require(registration != null);

            registration.IsActive = false;
            _registrationRepository.Update(registration);
            return _registrationRepository.SaveChanges();
        }
    }
}
